<template>
  <div class="container">
    <el-card class="box-card">
      <el-row>
        <el-col :span="5">
          <div class="grid-content bg-purple-dark"><span> </span></div>
        </el-col>
        <el-col :span="5">
          <div class="bg-purple-dark"><span> </span></div>
        </el-col>
        <el-col :span="4">
          <div class="grid-content bg-purple-dark">
            <el-popover placement="bottom" width="400" trigger="click" v-model="visible">
              <el-form
                :model="ruleForm"
                status-icon
                ref="ruleForm"
                :rules="rules"
                label-width="80px"
                label-position="left"
                class="demo-ruleForm"
              >
                <el-form-item label="管理员" prop="name">
                  <el-input v-model.number="ruleForm.name" placeholder="请输入管理员姓名"></el-input>
                </el-form-item>
                <el-form-item label="用户名" prop="username">
                  <el-input type="text" v-model="ruleForm.username" placeholder="请输入用户名"></el-input>
                </el-form-item>
                <el-form-item label="密码" prop="password">
                  <el-input type="text" v-model="ruleForm.password" placeholder="请输入密码"></el-input>
                </el-form-item>
              </el-form>
              <el-button size="mini" @click="visible = false">取消</el-button>
              <el-button type="primary" size="mini" @click="submitPass">确定</el-button>
              <el-button slot="reference" round>添加管理员</el-button>
            </el-popover>
          </div>
        </el-col>
        <el-col :span="8">
          <div class="grid-content bg-purple-dark">
            <el-input placeholder="请输入用户ID" v-model="input1" @keyup.enter.native="queryAdminId">
              <template slot="prepend">用户ID</template>
              <el-button slot="append" class="el-icon-search" @click="queryAdminId"></el-button>
            </el-input>
          </div>
        </el-col>
      </el-row>
    </el-card>

    <el-card>
      <el-table
        :data="tableData"
        highlight-current-row
        border
        style="width: 100%"
        :header-cell-style="{ 'text-align': 'center' }"
        :cell-style="{ 'text-align': 'center' }"
      >
        <el-table-column prop="id" label="用户ID" width="80"> </el-table-column>
        <el-table-column prop="name" label="姓名"> </el-table-column>
        <el-table-column prop="username" label="用户名"> </el-table-column>
        <el-table-column label="操作">
          <template slot-scope="scope">
            <div class="scope">
              <div class="warning">
                <el-popconfirm title="确定删除吗？">
                  <el-button slot="reference" size="mini" type="warning">删除</el-button>
                </el-popconfirm>
              </div>
              <div>
                <el-dialog title="编辑管理员信息" :visible.sync="isvisible" width="30%">
                  <el-form
                    :model="adminform"
                    status-icon
                    ref="ruleForm"
                    :rules="rules"
                    label-width="80px"
                    label-position="left"
                    class="demo-ruleForm"
                  >
                    <el-form-item label="管理员" prop="name">
                      <el-input v-model.number="adminform.name" placeholder="请输入管理员姓名"></el-input>
                    </el-form-item>
                    <el-form-item label="用户名" prop="username">
                      <el-input type="text" v-model="adminform.username" placeholder="请输入用户名"></el-input>
                    </el-form-item>
                  </el-form>
                  <el-button size="mini" @click="visible = false">取消</el-button>
                  <el-button type="primary" size="mini" @click="updataUser">确定</el-button>
                </el-dialog>
                <el-button size="mini" @click="editor(scope.row)">编辑</el-button>
              </div>
            </div>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
import { queryAllAdmin, addAdmin, queryId, updataAdmin } from '@/api/user'
export default {
  name: 'AdminUser',
  data() {
    return {
      adminform: {
        id: '',
        name: '',
        username: '',
        password: 123456
      },
      rules: {
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }],
        name: [
          { required: true, message: '请输入管理员姓名', trigger: 'blur' },
          { min: 2, max: 12, message: '长度在 2 到 12 个字符', trigger: 'blur' }
        ],
        username: [{ required: true, message: '请输入用户名', trigger: 'blur' }]
      },
      isvisible: false,
      visible: false,
      input1: '',
      ruleForm: {
        name: '',
        username: '',
        password: ''
      },
      tableData: []
    }
  },
  created() {
    this.getAllAdmin()
  },
  methods: {
    // 编辑按钮
    editor(row) {
      this.isvisible = true
      this.adminform.id = row.id
    },
    // 修改信息
    async updataUser() {
      const res = await updataAdmin({ ...this.adminform })
      if (res.data.statu == 200) {
        const user = this.$store.state.user.profile.id
        if (user == this.adminform.id) {
          this.$message.success('账户信息更新，请重新登录')
          this.$store.commit('user/deletetoken')
          this.$router.push('/login')
        } else {
          this.$message.success(res.data.msg)
          this.isvisible = false
          this.getAllAdmin()
        }
      } else {
        this.$message.warning(res.data.msg)
      }
    },
    //查询
    async queryAdminId() {
      const res = queryId({ id: this.input1 })
      res.then(a => {
        if (a.data.statu == 200) {
          this.tableData = []
          this.tableData.push(a.data.data)
        } else {
          this.$message.warning(a.data.msg)
        }
      })
    },
    // 添加
    submitPass() {
      this.$refs.ruleForm.validate(async valid => {
        if (valid) {
          const res = await addAdmin({ ...this.ruleForm })
          if (res.data.statu == 200) {
            this.$message.success(res.data.msg)
            this.visible = false
          } else {
            this.$message.warning(res.data.msg)
          }
        }
      })
    },
    async getAllAdmin() {
      const res = await queryAllAdmin()
      console.log(res);
      if (res.data.statu == 200) {
        this.tableData = res.data.data
      } else if (res.data.statu == 403) {
        this.$message.warning('未获得此权限')
        this.$router.push('/')
      }
    }
  },
  computed: {
    userinfo: function () {
      return this.$store.state.user.profile
    }
  }
}
</script>
<style lang="less" scoped>
.warning {
  padding-right: 10px;
}
.box-card {
  margin-bottom: 10px;
}
.scope {
  display: flex;
  justify-content: center;
}
</style>
